define(["jquery","layuiall","zTree","jqform","baidueditor","helper"],function ($,layui,zTree,jqform,UE,helper) {
    var form = layui.form;
    controller = {
        editapp:function () {
            jqform.init()
        },
        addapps:function () {
            UE.getEditor('container');
            UE.getEditor('buydesc');
            jqform.init()
            $("#app-photo .del-photos-btn").click(function () {
                $(this).parents("tr").eq($(this).index()).remove()
            });
            $("#app-photos .del-photos-btn").click(function () {
                $(this).parents("tr").eq($(this).index()).remove()
            });
            $("#test1").click(function () {
                uploadFiles({
                    url: '/admin/webupload/webuploader/multi/1/filetype/image/app/admin/tab/local',
                    file:function (res) {
                        var file = JSON.parse(res);
                        var str = '<tr>'+
                            '<td>'+
                            '<input type="text" class="layui-input" name="app_photo" value="'+file[0].filepath+'">'+
                            '</td>'+
                            '<td>'+
                            '<img width="80"  src="'+file[0].preview_url+'">'+
                            '</td>'+
                            '<td>'+
                            '<a href="javascript:;" class="layui-btn layui-btn-danger layui-btn-xs del-photos-btn">删除</a>'+
                            '</td>'+
                            '</tr>';
                        $('#app-photo').append(str);
                        $("#app-photo .del-photos-btn").click(function () {
                            $(this).parents("tr").eq($(this).index()).remove()
                        });
                        localStorage.removeItem("upload");
                    }
                });
            });
            $("#test2").click(function () {
                uploadFiles({
                    url: '/admin/webupload/webuploader/multi/1/filetype/image/app/admin/tab/local',
                    file:function (res) {
                        var file = JSON.parse(res);
                        var str = '<tr>'+
                            '<td>'+
                            '<input type="text" class="layui-input" name="app_photos[]" value="'+file[0].filepath+'">'+
                            '</td>'+
                            '<td>'+
                            '<img width="80"  src="'+file[0].preview_url+'">'+
                            '</td>'+
                            '<td>'+
                            '<a href="javascript:;" class="layui-btn layui-btn-danger layui-btn-xs del-photos-btn">删除</a>'+
                            '</td>'+
                            '</tr>';
                        $('#app-photos').append(str);
                        $("#app-photos .del-photos-btn").click(function () {
                            $(this).parents("tr").eq($(this).index()).remove()
                        });
                        localStorage.removeItem("upload");
                    }
                });
            });
            level({dataType: 'tree'});
            form.on('radio(isfree)', function(data){
                switch (parseInt(data.value)){
                    case 0:
                        $(".price").hide();
                        break;

                    case 1:
                        $(".price").show();
                        break
                }
            });
        },
        saveapps:function () {
            var ue =UE.getEditor('container');
            UE.getEditor('buydesc');
            $("#app-photo .del-photos-btn").click(function () {
                $(this).parents("tr").eq($(this).index()).remove()
            });
            $("#app-photos .del-photos-btn").click(function () {
                $(this).parents("tr").eq($(this).index()).remove()
            });
            $("#test1").click(function () {
                uploadFiles({
                    url: '/admin/webupload/webuploader/multi/1/filetype/image/app/admin/tab/local',
                    file:function (res) {
                        var file = JSON.parse(res);
                        var str = '<tr>'+
                            '<td>'+
                            '<input type="text" class="layui-input" name="app_photo" value="'+file[0].filepath+'">'+
                            '</td>'+
                            '<td>'+
                            '<img width="80"  src="'+file[0].preview_url+'">'+
                            '</td>'+
                            '<td>'+
                            '<a href="javascript:;" class="layui-btn layui-btn-danger layui-btn-xs del-photos-btn">删除</a>'+
                            '</td>'+
                            '</tr>';
                        $('#app-photo').append(str);
                        $("#app-photo .del-photos-btn").click(function () {
                            $(this).parents("tr").eq($(this).index()).remove()
                        });
                        localStorage.removeItem("upload");
                    }
                });
            });
            $("#test2").click(function () {
                uploadFiles({
                    url: '/admin/webupload/webuploader/multi/1/filetype/image/app/admin/tab/local',
                    file:function (res) {
                        var file = JSON.parse(res);
                        var str = '<tr>'+
                            '<td>'+
                            '<input type="text" class="layui-input" name="app_photos[]" value="'+file[0].filepath+'">'+
                            '</td>'+
                            '<td>'+
                            '<img width="80"  src="'+file[0].preview_url+'">'+
                            '</td>'+
                            '<td>'+
                            '<a href="javascript:;" class="layui-btn layui-btn-danger layui-btn-xs del-photos-btn">删除</a>'+
                            '</td>'+
                            '</tr>';
                        $('#app-photos').append(str);
                        $("#app-photos .del-photos-btn").click(function () {
                            $(this).parents("tr").eq($(this).index()).remove()
                        });
                        localStorage.removeItem("upload");
                    }
                });
            });
            jqform.init()
            level({dataType: 'tree'});
            form.on('radio(isfree)', function(data){
                switch (parseInt(data.value)){
                    case 0:
                        $(".price").hide();
                        break;
                    case 1:
                        $(".price").show();
                        break
                }
            });
        },
        appshonp:function () {
            var form = layui.form;
            //状态切换
            form.on('switch(app_status)', function(data){
                var status = 0;
                if(data.elem.checked){
                    status = 1
                }
                $.ajax({
                    type:'post',
                    data:{
                        id:data.value,
                        app_status:status
                    },
                    url:$(data.elem).attr("data-url"),
                    success:function (res) {
                        layer.msg(res.msg)
                    }
                })
            });
            jqform.delete()

            //数据全部选中
            form.on('checkbox(select-all)', function(data){

                if(data.elem.checked==true){
                    $("input[name='idList[]']").prop('checked',true)
                }else {
                    $("input[name='idList[]']").prop('checked',false)
                }
                form.render('checkbox');
            });
        },

        addappversion:function () {
            var ue =UE.getEditor('container');
            jqform.init()
            $("#app-pack .del-photos-btn").click(function () {
                $(this).parents("tr").eq($(this).index()).remove()
            });
            $("#test3").click(function () {
                uploadFiles({
                    url: '/admin/webupload/webuploader/multi/1/filetype/file/app/admin/tab/local',
                    file:function (res) {
                        var file = JSON.parse(res);
                        var str = '<tr>'+
                            '<td>'+
                            '<input type="text" class="layui-input" name="app_pack" value="'+file[0].filepath+'">'+
                            '</td>'+
                            '<td>'+
                            '<img width="80"  src="'+file[0].preview_url+'">'+
                            '</td>'+
                            '<td>'+
                            '<a href="javascript:;" class="layui-btn layui-btn-danger layui-btn-xs del-photos-btn">删除</a>'+
                            '</td>'+
                            '</tr>';
                        $('#app-pack').append(str);
                        $("#app-pack .del-photos-btn").click(function () {
                            $(this).parents("tr").eq($(this).index()).remove()
                        });
                        localStorage.removeItem("upload");
                    }
                });
            });
           /* uploadFile({
                elm:'#test3',
                url:"/admin/webupload/uploadapp",
                table:'#app-pack',
                multiple:false,
                exts:'jpg|png|bmp|jpeg|avg',
                inputName:'app_pack'
            });*/
        },
        saveappversion:function () {
            var ue =UE.getEditor('container');
            jqform.init()
            $("#app-pack .del-photos-btn").click(function () {
                $(this).parents("tr").eq($(this).index()).remove()
            });
            $("#test3").click(function () {
                uploadFiles({
                    url: '/admin/webupload/webuploader/multi/1/filetype/file/app/admin/tab/local',
                    file:function (res) {
                        var file = JSON.parse(res);
                        var str = '<tr>'+
                            '<td>'+
                            '<input type="text" class="layui-input" name="app_pack" value="'+file[0].filepath+'">'+
                            '</td>'+
                            '<td>'+
                            '<img width="80"  src="'+file[0].preview_url+'">'+
                            '</td>'+
                            '<td>'+
                            '<a href="javascript:;" class="layui-btn layui-btn-danger layui-btn-xs del-photos-btn">删除</a>'+
                            '</td>'+
                            '</tr>';
                        $('#app-pack').append(str);
                        $("#app-pack .del-photos-btn").click(function () {
                            $(this).parents("tr").eq($(this).index()).remove()
                        });
                        localStorage.removeItem("upload");
                    }
                });
            });
        },
        appversion:function () {
            var form = layui.form;
            jqform.delete()
            form.on('checkbox(select-all)', function(data){

                if(data.elem.checked==true){
                    $("input[name='idList[]']").prop('checked',true)
                }else {
                    $("input[name='idList[]']").prop('checked',false)
                }
                form.render('checkbox');
            });
        },
    }
//层级选择
    function level(datas) {
        $(".menuLevel").click(function () {
            var TreeHtml = '<ul id="MenuTree" class="ztree"></ul>';
            var elm = $(this);
            var url = elm.attr("data-url");
            var role_id = [];
            var categoryList = [];
            var menuTreeAler = layer.open({
                type: 1,
                title: "分类选择",
                content: TreeHtml,
                area:["350px","500px"],
                shadeClose:true,
                btn:["确定"],
                success: function () {
                    $.ajax({
                        type:"get",
                        url :url,
                        dataType:'json',
                        data: datas,
                        success:function (res) {
                            var zTreeObj;
                            var setting = {
                                view: {
                                    showIcon: true
                                },
                                data: {
                                    simpleData: {
                                        enable: true,
                                        idKey:"id",
                                        pIdKey:"parent_id",
                                    },
                                    key:{
                                        name:"name"
                                    }
                                },
                                check: {
                                    enable: true,
                                    chkStyle: "checkbox",
                                    radioType: "all"
                                },
                                callback: {
                                    onClick: function (e, treeId, treeNode, clickFlag) {
                                        zTreeObj.checkNode(treeNode, !treeNode.checked, true);
                                        role_id = [];
                                        categoryList=[]
                                        nodes = zTreeObj.getCheckedNodes(true)
                                        for (var i = 0; i < nodes.length; i++) {
                                            role_id.push(nodes[i].id)
                                            categoryList.push(nodes[i].name)
                                        }
                                    },
                                    onCheck:function zTreeOnCheck(event, treeId, treeNode) {
                                        role_id = [];
                                        categoryList=[]
                                        nodes = zTreeObj.getCheckedNodes(true)
                                        for (var i = 0; i < nodes.length; i++) {
                                            role_id.push(nodes[i].id)
                                            categoryList.push(nodes[i].name)
                                        }

                                    },
                                }
                            };
                            zTreeObj = $.fn.zTree.init($("#MenuTree"), setting, res);
                            zTreeObj.expandAll(true);
                        }
                    })
                },
                yes:function () {
                    $("input[name='category_id']").val(role_id.join(","))
                    elm.val(categoryList.join(","))
                    layer.close(menuTreeAler)
                }
            })
        })
    }
    return controller;
})